<?PHP
	/************************************************************************
	*       This file, in addition to all other parts of X7 Chat are        *
	*                   Copyright 2003-2007 by Tim Chamness.                *
	* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
	* Version:          X7 Chat 3.0.0-A2                                    *
	* Release Date:     October 20, 2007                                    *
	* Author:           Tim Chamness                                        *
	* Copyright:        2003-2007 by Tim Chamness                           *
	* Website:          http://www.x7chat.com/                              *
	* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
	* X7 Chat 3 is free software; you can redistribute it and/or modify     *
	* it under the terms of the GNU General Public License as published by  *
	* the Free Software Foundation; either version 3 of the License, or     *
	* (at your option) any later version.                                   *
	*                                                                       *
	* X7 Chat 3 is distributed in the hope that it will be useful, but      *
	* WITHOUT ANY WARRANTY; without even the implied warranty of            *
	* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
	* General Public License for more details.                              *
	*                                                                       *
	* You should have received a copy of the GNU General Public License     *
	* along with this program.  If not, see <http://www.gnu.org/licenses/>. *
	*                                                                       *
	* Additional Terms, compatible with Section 7 of the GNU GPL v3,        *
	* apply to X7 Chat 3 and all X7 Chat 3 files, including this one.       *
	* You should have received a copy of these Additional Terms in the      *
	* file docs/COPYING.ADDITIONAL_TERMS.  If you did not receive a copy    *
	* please visit <http://www.x7chat.com/COPYING.ADDITIONAL_TERMS/>.       *
	************************************************************************/
	
	// Make sure this file isn't being accessed directly
	if(!defined('IN_X7CHAT3')) exit;
	
	// See if a code is being passed
	$uid = lib_input::read('uid','',lib_input::NUMBER);
	$act = lib_input::read('code','');
	
	if(!empty($uid) && !empty($act)){
		
		// See if this code exists in the db
		$user = new lib_user($uid);
		if(strlen($act) != 40 || !$user->is_valid() || $user->data_cache['activation_code'] != $act){
			
			if($user->is_valid() && $user->data_cache['activation_code'] == 0)
				$msg = $GLOBALS['LANG']->lang_intro->already_activated;
			elseif($user->is_valid() && strlen($user->data_cache['activation_code']) != 40)
				$msg = $GLOBALS['LANG']->lang_intro->activation_reqs_admin;
			else
				$msg = $GLOBALS['LANG']->lang_intro->wrong_activation;
				
			echo tpl_verify::activation_failed($msg);
			return;
		}
		
		$user->data_cache['activation_code'] = 0;
		$user->commit_changes();
		$GLOBALS['DATABASE']->remove('activation',$uid);
		
		echo tpl_verify::account_activated($GLOBALS['LANG']->lang_intro->account_active);
		return;
	}
	
	$form = new x7form();
	$form->add_section(1,$GLOBALS['LANG']->lang_intro->verify,$GLOBALS['LANG']->lang_intro->activation_instructions);
	$form->add_field_text(1,'email',$GLOBALS['LANG']->lang_intro->email);
	
	$res = $form->get_results();
	if(!empty($res)){
		
		$user = new lib_user($res['email'],'email');
		if(!$user->is_valid()){
			$form->add_field_error('email',$GLOBALS['LANG']->lang_intro->email_not_found,'');
		}elseif($user->data_cache['activation_code'] == 0){
			$form->add_section_error(1,$GLOBALS['LANG']->lang_intro->already_activated);
		}elseif(strlen($user->data_cache['activation_code']) != 40){
			$form->add_section_error(1,$GLOBALS['LANG']->lang_intro->activation_reqs_admin);
		}else{
			// Account activation e-mail sent
			$check_email = lib_user_extended::send_welcome_email($user);
			
			if($check_email == lib_email::E_NONE){
				$msg = $GLOBALS['LANG']->lang_intro->activation_email_sent;
			}else{
				$msg = $GLOBALS['LANG']->format($GLOBALS['LANG']->lang_intro->resend_activation_failed,array('ecode',$check_email));
			}
			
			echo tpl_verify::submition_ok($msg);
			return;
		}
		
	}
	
	// Output a form
	echo tpl_verify::verify_form($form);
	return;
?>